package com.cloud.springcloudhysyrix.consumer;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.web.client.RestTemplate;

/**
 *  方式二：
 * 在这里实现手动的方式完成@HystrixCommand注解所要完成的事情。
 *
 *
 *
 * weiximei on 2018/5/6
 */
public class HelloServiceCommand extends HystrixCommand<String> {

    private RestTemplate restTemplate;

    // 由于这个构造方法无法满足，在下面进行重构
//    protected HelloServiceCommand(HystrixCommandGroupKey group) {
//        super(group);
//    }

    protected HelloServiceCommand(String commandGroupKey,RestTemplate restTemplate) {
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
    }

    @Override
    protected String run() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }

    @Override
    protected String getFallback() {
        return "error";
    }
}
